package com.canbot.u05.adapter;

import android.app.ActionBar;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.bean.AdvisoryQA;
import com.canbot.u05.bean.Merchandise;
import com.canbot.u05.utils.Logger;

import java.util.Collections;
import java.util.List;

/**
 * 咨询问答设置数据界面
 */

public class MerchandiseAdapter extends RecyclerView.Adapter<MerchandiseAdapter.ViewHolder> implements View.OnClickListener {

        private Context context;

        private List<Merchandise> data;

        private LayoutInflater mInflater;

        private PopupWindow popupWindow;

        private RecyclerView listView;

        public MerchandiseAdapter(Context context, List<Merchandise> data,RecyclerView listView) {
                this.context = context;
                this.listView = listView;
                this.mInflater = LayoutInflater.from(context);
                this.data = Collections.synchronizedList(data);
        }

        /**
         * 弹窗显示菜单
         *
         * @param view
         * @param position
         */
        public void showPopupWindow(View view, int position) {

                View contentview = LayoutInflater.from(context).inflate(R.layout.menu_setting_merchandise, null);

                // 设置按钮的点击事件
                Button btnRename = (Button) contentview.findViewById(R.id.btn_rename);
                Button btnEdit = (Button) contentview.findViewById(R.id.btn_edit);
                Button btnDel = (Button) contentview.findViewById(R.id.btn_delet);

                btnRename.setOnClickListener(this);
                btnEdit.setOnClickListener(this);
                btnDel.setOnClickListener(this);

                // 显示具体名称
                TextView textView = (TextView) contentview.findViewById(R.id.title);
                textView.setText(data.get(position).getTitle());

                popupWindow = new PopupWindow(contentview,
                        (int)context.getResources().getDimension(R.dimen.x300), ActionBar.LayoutParams.WRAP_CONTENT, true);

                popupWindow.setTouchInterceptor(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                Log.i("mengdd", "onTouch : ");
                                return false;
                                // 这里如果返回true的话，touch事件将被拦截
                                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss

                        }
                });
                int visibleLastPosi = -1;
                RecyclerView.LayoutManager layoutManager = listView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                        LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                        //获取最后一个可见view的位置
                        visibleLastPosi = linearManager.findLastVisibleItemPosition();
                }
                // 设置好参数之后再show
                int[] location = new int[2];
                view.getLocationOnScreen(location);
                if (position <= visibleLastPosi-4){
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x40));
                }else {
                        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
                        popupWindow.setBackgroundDrawable(context.getResources().getDrawable(
                                R.drawable.menu_background2));
                        popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x260));
                }
//                popupWindow.showAsDropDown(view,-(int)context.getResources().getDimension(R.dimen.x279),-(int)context.getResources().getDimension(R.dimen.x30));

                if (listner != null) {
                        listner.showPopuwindow(position);
                }


        }

        MerchandiseOnclickListner listner;

        public void setListener(MerchandiseOnclickListner listner) {
                this.listner = listner;
        }

        public interface MerchandiseOnclickListner {

                void rename();

                void edit();

                void del();

                void showPopuwindow(int position);

        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.btn_rename:

                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.rename();
                                        }
                                }
                                break;
                        case R.id.btn_edit:
                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.edit();
                                        }
                                }
                                break;

                        case R.id.btn_delet:

                                if (popupWindow != null) {
                                        popupWindow.dismiss();
                                        if (listner != null) {
                                                listner.del();
                                        }
                                }
                                break;
                }

        }

         class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

                RelativeLayout relativeLayout;

                TextView tvName;

                ImageView ivMenu;

                 public int position;

                 public ViewHolder(View itemView) {
                         super(itemView);
                         relativeLayout = (RelativeLayout) itemView.findViewById(R.id.rl_item_onclick);
                         tvName = (TextView) itemView.findViewById(R.id.text1);
                         ivMenu = (ImageView)  itemView.findViewById(R.id.img1);
                         relativeLayout.setOnClickListener(this);
                 }

                @Override
                public void onClick(View v) {
                        switch (v.getId()){

                                case R.id.rl_item_onclick:
                                        showPopupWindow(ivMenu,position);
                                        break;
                        }
                }
        }


        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View inflate = mInflater.inflate(R.layout.sales_setting_item, parent, false);
                return new MerchandiseAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
                holder.position = position;
                Merchandise merchandise = data.get(position);
                holder.tvName.setText(merchandise.getTitle());
        }

        @Override
        public long getItemId(int postion) {
                return postion;
        }

        @Override
        public int getItemViewType(int position) {
                return data.get(position).getType();
        }

        @Override
        public int getItemCount() {
                return data.size();
        }

}
